#!/usr/bin/env python
# -*- coding:utf-8 -*-
import ConfigParser

#@wrapper_login
def shopping():
    ShopInfoFile=ConfigParser.ConfigParser()
    ShopInfoFile.read('info.txt')
    GoodsList=ShopInfoFile.options('shoplist')
    Balance=ShopInfoFile.getfloat('login','balance')
    while True:
        print "您的信用卡可用消费：%d元" % Balance
        for num,goods in enumerate(GoodsList,1):    #打印商品列表
            print num,goods,'售价:%s元'% ShopInfoFile.get('shoplist',goods)
        print "0 退出"
        UserSelectNum=int(raw_input("选择商品编号购买:"))
        if UserSelectNum==0:break
        UserSelectGoods=GoodsList[UserSelectNum-1]
        UserSelectGoodsPrice=ShopInfoFile.getfloat('shoplist',UserSelectGoods)
        if Balance-UserSelectGoodsPrice >=0:
            print "购买成功!"
            Balance=Balance-UserSelectGoodsPrice
            ConsumeRecord="time,%s,%s元\n"%(UserSelectGoods,UserSelectGoodsPrice)
            with open('record','a')as fa:
                fa.write(ConsumeRecord)
        else:
            print "购买失败，余额不足！"
    ShopInfoFile.set('login','balance',Balance)
    ShopInfoFile.write(open('info.txt','w'))    #写入消费记录文件

shopping()